import JZOffer.ListNode;
import JZOffer.MySingleList;
import exercise.L230701_II;

import java.io.*;
import java.lang.reflect.Field;
import java.util.*;


class O {
    public String O() {
        return "[o.O] -> O.o";
    }
}

public class Test {
    private static ListNode mergeTwo(ListNode cur1, ListNode cur2) {
        ListNode node = new ListNode(-1);
        ListNode cur = node;
        while (cur1 != null || cur2 != null) {
            if (cur1 == null) {
                cur.next = cur2;
                break;
            }
            if (cur2 == null) {
                cur.next = cur1;
                break;
            }

            if (cur1.val <= cur2.val) {
                cur.next = cur1;
                cur1 = cur1.next;
            } else {
                cur.next = cur2;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        return node.next;
    }

    public static void main1(String[] args) {
        MySingleList list1 = new MySingleList();
        MySingleList list2 = new MySingleList();
        list1.addLast(1);
        list1.addLast(3);
        list1.addLast(5);
        list2.addLast(2);
        list2.addLast(4);
        list2.addLast(6);
        ListNode node = mergeTwo(list1.head, list2.head);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }


    public static void main2(String[] args) {
        String s1 = "..2";
        s1 = s1.replaceFirst("\\.", "");
        String[] s = "3.".split("\\.");
        System.out.println(s1.contains("."));
    }

    public static void main3(String[] args) {
        System.out.println((int) Integer.MAX_VALUE);
    }


    public static void main4(String[] args) {

        String[] strings = new String[]{"3", "30", "34", "5", "9"};

        Arrays.sort(strings, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String s1 = o1 + o2;
                String s2 = o2 + o1;
                return Integer.parseInt(s1) > Integer.parseInt(s2) ? 1 : -1;
            }
        });
    }

    public static void main5(String[] args) {
        System.out.println(3 >>> 1);
    }

    interface Com {
        int M = 200;

        int f();
    }

    class ImpCom implements Com {
        @Override
        public int f() {
            return M + 100;
        }
    }

    public static void main6(String[] args) {
        char ch = 'g';
        String s = Character.toString(ch);
        System.out.println(s);
    }

    public static void main7(String[] args) {
        int[] arr = {2, 0, 1};

        L230701_II s = new L230701_II();
        s.sortColors(arr);
        System.out.println(Arrays.toString(arr));
    }

    public int add(int a, int b) {
        try {
            return a + b;
        } catch (Exception e) {
            System.out.println("catch语句块");
        } finally {
            System.out.println("finally语句块");
        }
        return 0;
    }

    public static void main8(String[] args) {
        Test test = new Test();
        System.out.println("和是：" + test.add(9, 34));
    }


    public static void main9(String[] args) {
        TreeSet<Integer> set = new TreeSet<Integer>();
        TreeSet<Integer> subSet = new TreeSet<Integer>();
        for (int i = 606; i < 613; i++) {
            if (i % 2 == 0) {
                set.add(i);
            }
        }
        subSet = (TreeSet) set.subSet(608, true, 611, true);
        set.add(609);
        System.out.println(set + " " + subSet);
    }

    public static void main10(String[] args) {
        O o = new O();
        System.out.println(o.O());


    }

    static class A {
        void print() {
            System.out.println("a");
        }
    }


    static class B extends A {
        void print() {
            System.out.println("b");
        }
    }


    static class C extends A {
        void print() {
            System.out.println("c");
        }
    }

    static void myPrint(A a) {
        a.print();
    }

    public static void main11(String[] args) {
        B b = new B();
        C c = new C();

        myPrint(b);
        myPrint(c);
    }

    public static void main12(String[] args) {
//        Student student = new Student();
        Student student = new Student("zhangsan", 11);
        System.out.println(student.getName() + student.getAge());

    }

    public static void main13(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("D:/test.txt");
        while (true) {
            int b = inputStream.read();
            if (b == -1) {
                // 文件内容已经读取完毕
                return;
            }
            System.out.println(b);
        }

    }

    public static void main14(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("D:/test.txt");
        while (true) {
            byte[] buffer = new byte[1024];
            int len = inputStream.read(buffer);
            if (len == -1) {
                // 文件读取完毕
                break;
            }
            for (int i = 0; i < len; i++) {
                System.out.println(buffer[i]);
            }
        }
        inputStream.close();
    }

    public static void main15(String[] args) throws IOException {
        try (OutputStream outputStream = new FileOutputStream("D:/test.txt")) {
            outputStream.write(97);
            outputStream.write(98);
            outputStream.write(99);
            outputStream.write(100);
            outputStream.flush();
        }
    }

    public static void main16(String[] args) throws IOException {
        try (Reader reader = new FileReader("D:/test.txt")) {
//            while (true) {
//                int ch = reader.read();
//                if (ch == -1) {
//                    break;
//                }
//                System.out.println("" + (char) ch);
//            }

            while (true) {
                char[] buffer = new char[1024];
                int len = reader.read(buffer);
                if (len == -1) {
                    break;
                }
                for (int i = 0; i < len; i++) {
                    System.out.println(buffer[i]);
                }
            }
        }
    }

    public static void main17(String[] args) throws IOException {
        try (Writer writer = new FileWriter("D:/test.txt")) {
            writer.write("hello world");
            // 手动刷新缓冲区
            writer.flush();
        }
    }

    public static void main18(String[] args) throws IOException {
        try (OutputStream outputStream = new FileOutputStream("D:/test.txt");
             PrintWriter printWriter = new PrintWriter(outputStream)) {
            printWriter.println("你好, PrintWriter");
        }
    }

    public static void main19(String[] args) {
        int[][] arr = new int[10][10];
        Arrays.fill(arr, 100);
        for (int[] x : arr) {
            for (int xx : x) {
                System.out.println(xx);
            }
        }
    }

    public static void swap(Integer a, Integer b) {
        int tmp = a;
        Class<? extends Integer> integerClass = a.getClass();
        try {
            Field value = integerClass.getDeclaredField("value");
            value.setAccessible(true);
            value.setInt(a, b);
            value.setInt(b, tmp);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public interface IShape {
        void draw();
    }

    public static class Circle implements IShape {

        @Override
        public void draw() {
            System.out.println("circle");
        }
    }

    public static class Square implements IShape {

        @Override
        public void draw() {
            System.out.println("square");
        }
    }

    public static class ShapeFactory {
        public static IShape getShape(String shape) {
            if (shape.equalsIgnoreCase("circle")) {
                return new Circle();
            } else if (shape.equalsIgnoreCase("square")) {
                return new Square();
            }
            return null;
        }
    }

    public static void main20(String[] args) {
        IShape shape1 = ShapeFactory.getShape("Circle");
        IShape shape2 = ShapeFactory.getShape("Square");
        shape1.draw();
        shape2.draw();
    }

    public static volatile int count = 0;

    public static void main21(String[] args) {
        Object locker = new Object();

        Thread t1 = new Thread(() -> {
            while (true) {
                synchronized (locker) {
                    while (count % 3 != 0) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("A");
                    count++;
                    locker.notifyAll();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                synchronized (locker) {
                    while (count % 3 != 1) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("B");
                    count++;
                    locker.notifyAll();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            while (true) {
                synchronized (locker) {
                    while (count % 3 != 2) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    count++;
                    locker.notifyAll();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode l = pHead;
        ListNode r = pHead;
        l = l.next;
        r = r.next.next;

        while (l != null && r.next != null && l != r) {
            l = l.next;
            r = r.next.next;
        }

        if (l == r && l != null) {
            l = pHead;

            while(l != r) {
                l = l.next;
                r = r.next;
            }
            return l;
        }
        return null;
    }
    public static void main22(String[] args) {
        final int a = 0;
        // a = 2;
    }

    public static void main23(String[] args) {
        // 冒泡排序
        int[] arr = {6,5,2,1,7,9};

        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1; j++) {
                if (arr[j] > arr[j+1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    public static void main24(String[] args) {
//        String a = "abc";
//        String b = "abc";
//        System.out.println(a == b);
        Integer x1 = 10;
        Integer x2 = 10;
        System.out.println(x1 == x2);

        Integer x3 = 200;
        Integer x4 = 200;
        System.out.println(x3 == x4);
    }

    public static void main888(String[] args) {
        String s = "asdsasd";

    }

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();

        map.put("k1", "v1");
        map.put("k2", "v2");
        map.put("k3", "v3");



    }

}

interface If1 {

}

interface If2 {

}

interface If3 extends If1, If2 {

}

class Student {
    private String name;
    private int age;

    public Student() {
        System.out.println("无参");
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class MyClass {
    private int instanceVar = 10;

    public void instanceMethod() {
        System.out.println("This is an instance method.");
    }

    public static void staticMethod() {
        MyClass obj = new MyClass();
        System.out.println(obj.instanceVar);  // 访问实例变量
        obj.instanceMethod();                // 调用实例方法
    }
}


class DBOptions {
    private int port = 3306;
    private String user = "root";
    private String password = "123456";

    public void setPort(int port) {
        this.port = port;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

class Animal {
    public String name;

    public Animal(String name) {
        this.name = name;
    }

    public void speak() {
        System.out.println(name + "speak");
    }
}

class Cat extends Animal {
    public String name;
    public Cat(String name) {
        super(name);
        this.name = name;
    }

    @Override
    public void speak() {
        System.out.println(this.name + "speak");
    }
}

class Tess {

    public void fn() {

    }

    public static void speak(Animal animal) {
        animal.speak();
    }

    public static void main(String[] args) {
        Cat cat = new Cat("cat");
        speak(cat);
    }
}

class Tessss {
    public static void main(String[] args) {

    }
}

class Singleton {
    private static volatile Singleton singleton;

    private Singleton(){}

    public Singleton getInstance() {
        if (singleton == null) {
            synchronized (this) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
